{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第10章 正则和JSON"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关于正则表达式可以参考如下两篇文章：\n",
    "\n",
    "+ [Python正则表达式指南](https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html)\n",
    "+ [Python正则表达式](http://www.runoob.com/python/python-reg-expressions.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关于JSON\n",
    "\n",
    "### 反序列化\n",
    "\n",
    "```python\n",
    "json_str='{\"name\":\"小气\",\"age\"：18}' #JSON 字符串\n",
    "json_array='[{\"name\":\"小气\",\"age\"：18},{\"name\":\"小气\",\"age\"：18}]' # JSON数组\n",
    "```\n",
    "\n",
    "+ 里面是双引号的话，外面必须是单引号\n",
    "+ JSON格式的字符串键值必须是双引号,数字可以不加双引号\n",
    "+ json字符串loads，转换成dict字典；jsonarray数组loads，转换成列表\n",
    "+ JSON字符串到某种语言的解析过程叫反序列化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'name': '小气', 'age': 18}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "json_str='{\"name\":\"小气\",\"age\":18}' #JSON 字符串\n",
    "student = json.loads(json_str) # json字符串loads，转换成dict字典\n",
    "print(type(student))\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "[{'name': '小气', 'age': 18}, {'name': '小气', 'age': 18}]\n"
     ]
    }
   ],
   "source": [
    "json_array='[{\"name\":\"小气\",\"age\":18},{\"name\":\"小气\",\"age\":18}]' # JSON数组\n",
    "student_list = json.loads(json_array) # jsonarray数组loads，转换成列表\n",
    "print(type(student_list))\n",
    "print(student_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 序列化\n",
    "\n",
    "+ 序列化是将python 数据类型转化成JSON数据类型\n",
    "+ 反序列化是将JSON数据类型转化成python数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "[{\"name\": \"\\u76f4\\u64ad\", \"age\": 18, \"flag\": false}, {\"name\": \"\\u5c0f\\u738b\", \"age\": \"09\"}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "array=[{'name':'直播','age':18,'flag':False},{'name':'小王','age':'09'}]\n",
    "json_str=json.dumps(array)\n",
    "print(type(json_str))\n",
    "print(json_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### json的概念澄清\n",
    "\n",
    "注意对JSON的理解的误区。应该跳出特定语言的范畴来理解JSON. (JSON并不是专属于特定语言里的，如JavaScript)  \n",
    "JSON和JavaScript一样，是W3C标准script（ECMA Script）的实现方式之一  \n",
    "由于JS是目前主流的Web前端语言，而JSON被大量用于JavaScript的交互层, 因此常被误解为附属于JavaScript  \n",
    "+ JSON：传输数据的一种格式，中间数据类型\n",
    "+ JSON对象：放在JS当中成立；python里没有JSON对象\n",
    "+ JSON字符串：符合 JSON 格式的字符串"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
